package pay.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pay.service.PayService;
import pay.utils.R;

import java.util.Map;

@RestController
@RequestMapping("/pay")
@Tags(value = {@Tag(name = "支付模块", description = "支付模块接口")})
public class PayController {

    @Autowired
    private PayService payService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/notify_url")
    @Tags(value = {@Tag(name = "支付模块", description = "支付回调地址")})
    public R notifyUrl(@RequestBody Map<String, Object> data) throws Exception {
        try {
            payService.handleCallBack(data);
            return R.ok("<xml>\n" +
                    "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                    "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                    "</xml>\n");
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok("<xml>\n" +
                    "  <return_code><![CDATA[FAIL]]></return_code>\n" +
                    "  <return_msg><![CDATA[签名失败]]></return_msg>\n" +
                    "</xml>\n");
        }
    }

}
